
import vm from "../main.js";
import axios from "axios";
import Qs from "qs";
import router from "../router";

let baseUrl = "http://47.105.206.10:7087/" ;// 请求接口路径
let baseUrlUpLoad = "http://47.105.206.10:7087/"; // 上传文件专属路径


let baseUrlServe = "/api"; // 请求接口代理路径
let baseUrlServeUpLoad = "/upload"; // 文件上传代理路径

axios.defaults.baseURL = baseUrlServe;
axios.defaults.timeout = 60000;

function request(config){
  let instence = axios.create({
    headers:{
      'Auth': window.localStorage.getItem('Token'),
      'ContentType': 'application/x-www-form-urlencoded'
    },
    transformRequest: [function(data) {
      data = Qs.stringify(data);
      return data;
    }]
  })

  //添加一个请求拦截
axios.interceptors.request.use(function (res) {
  // Do something before request is sent
  //添加loading
  // vm.$store.commit('handleLoding', { status: true });
  return res;
  }, function (error) {
    // Do something with request error
    // vm.$store.commit('handleLoding', { status: false });
    return error;
  });

  // 添加一个响应拦截
  axios.interceptors.response.use(function (res) {
    console.log('响应:', res.config.url, res)
    // Do something with response data
    //关闭loading
    // vm.$store.commit('handleLoding', { status: false });
    return res.data;
  }, function (error) {
    // Do something with response error
    // vm.$store.commit('handleLoding', { status: false });
    return error;
  });

  return instence(config)
}

function requestSys(config) {
  let urlStart = ''
  config.url = urlStart + config.url
  let instence = axios.create({
    headers: {
      'Auth': window.localStorage.getItem('Token'),
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    transformRequest: [function (data) {
      data = Qs.stringify(data);
      return data;
    }],
  });
  instence.interceptors.request.use(function (res) {
    //请求开始
    return res;
  }, function (err) {
    //
    // vm.$store.commit('handleLoding', { status: false });
    return err;
  });
  instence.interceptors.response.use(function (res) {
    //响应成功
    //关闭loading
    // vm.$store.commit('handleLoding', { status: false });
    if(res.data.flag == 2){
      window.localStorage.removeItem('Token');
      vm.$message({
           message: res.data.message,
           type: 'error'
      })
      router.replace("/login");
    }
    return res.data;
  }, function (err) {
    
    // vm.$store.commit('handleLoding', { status: false });
    return err;
  });
  return instence(config)
}

export  {
  request,
  requestSys,
  baseUrl,
  baseUrlUpLoad,
}